package com.symc.sims0213xfw.controller.student;

import com.symc.sims0213xfw.bean.Classes;
import com.symc.sims0213xfw.service.OperationStatusResult;
import com.symc.sims0213xfw.service.impl.ClassesServiceImpl;
import com.symc.sims0213xfw.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author: 凤文  沈阳医学院2019级医学信息工程 0213
 * @CreateTime: 2022/01/22 22:52
 * @Description:
 */
@WebServlet("/addStudentServlet")
public class AddStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();

        String departmentName = req.getParameter("selectedDepartment");
        String grade = req.getParameter("selectedGrade");
        String classesName = req.getParameter("selectedClasses");

        if (departmentName.equals("--请选择学院--")){
            out.write("您未选择学院！");
            out.write("<br>2秒后自动跳转到添加页面。。。");
            String requestPath = req.getContextPath() + "/jsp/student/addStudent.jsp";
            resp.setHeader("refresh", "2;url="+requestPath);
            return;
        }
        if (grade.equals("--请选择年级--")){
            out.write("您未选择年级！");
            out.write("<br>2秒后自动跳转到添加页面。。。");
            String requestPath = req.getContextPath() + "/jsp/student/addStudent.jsp";
            resp.setHeader("refresh", "2;url="+requestPath);
            return;
        }
        if (classesName.equals("--请选择班级--")){
            out.write("您未选择班级！");
            out.write("<br>2秒后自动跳转到添加页面。。。");
            String requestPath = req.getContextPath() + "/jsp/student/addStudent.jsp";
            resp.setHeader("refresh", "2;url="+requestPath);
            return;
        }

        String classId = new ClassesServiceImpl().getClassesId(departmentName,
                Integer.parseInt(grade),
                classesName).getId();
        String id = req.getParameter("id");
        String name = req.getParameter("name");

        Long status = new StudentServiceImpl().addStudent(id, name, classId);

        if (status == OperationStatusResult.ID_LENGTH_OUT_OF_BOUNDS) {
            out.write("输入的学号太长啦？？？");
        } else if (status == OperationStatusResult.NAME_LENGTH_OUT_OF_BOUNDS) {
            out.write("输入的姓名太长啦？？？");
        } else if (status == OperationStatusResult.CLASS_ID_LENGTH_OUT_OF_BOUNDS) {
            out.write("输入的班级号太长啦？？？");
        } else if (status == OperationStatusResult.EXIST) {
            out.write("用户已存在？？？");
        } else if (status == OperationStatusResult.SUCCESS) {
            out.write("添加成功！");
        } else if (status == OperationStatusResult.DEFEAT) {
            out.write("添加失败！");
        }
        out.write("<br>2秒后自动跳转到查看页面。。。");
        resp.setHeader("refresh", "2;url=/SMIS0213xfw//listStudentViewServlet");
    }
}
